package com.fansz.FanszAPI.web;

import com.fansz.FanszAPI.common.unifystructre.UnifyResponse;
import com.fansz.FanszAPI.domain.*;
import com.fansz.FanszAPI.service.BaseService;
import com.fansz.FanszAPI.service.StartSendMsg;
import com.fansz.FanszAPI.utils.Constants;
import com.fansz.FanszAPI.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import com.fansz.FanszAPI.common.BaseController;
import com.fansz.FanszAPI.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作接口类
 */
@RestController
@Component
@RequestMapping("/user")
public class UserController extends BaseController {

    private static Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    UserService userService;
    
    @RequestMapping("/")
    public String Index(){
        return "hello world!";
    }

    /**
     * 查询用户接口
     * @param id 用户名
     * @param sessionId sessionId
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @return resp 返回对象
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse getUser(@RequestParam(value="id", required = false) String id,
                                  @RequestParam(value="sessionId", required = false) String sessionId,
                                  @RequestParam(value="deviceId", required = false) String deviceId,
                                  @RequestParam(value="timeStamp", required = false) String timeStamp)
    {
        logger.info("-----[findUser: id ： " + id + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp + "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!findUserParaCheck(id, sessionId, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            User user = userService.findByUserId(id);

            if(user == null)
            {
                resp.setCode(Constants.USER_NOT_EXSIT);
                resp.setInfo(Constants.USER_NOT_EXSIT_INFO);
                return resp;
            }

            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            resp.setContent(user);
            logger.info("-----[findUser response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("findUser error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
            return resp;
        }
        return resp;
    }

    /**
     *
     * 查询用户接口参数验证
     */
    private boolean findUserParaCheck(String id, String sessionId, String deviceId, String timeStamp)
    {
        boolean result = true;

        if (StringUtils.isEmpty(id))
        {
            result = false;
        }
        if (StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }
        return result;
    }

    /**
     * 用户注册接口
     * @param name 用户名
     * @param deviceId 客户端设备id
     * @param identityCode 验证码
     * @param timeStamp 当前时间
     * @param password 用户密码
     * @param type 用户名类型
     * @return resp 返回对象
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse register(@RequestParam(value="name", required = false) String name,
                                  @RequestParam(value="deviceId", required = false) String deviceId,
                                  @RequestParam(value="identityCode", required = false) String identityCode,
                                  @RequestParam(value="timeStamp", required = false) String timeStamp,
                                  @RequestParam(value="password", required = false) String password,
                                  @RequestParam(value="type", required = false) String type){
        logger.info("-----[register: name : " + name + ", password : " + password + ", identityCode : " + identityCode + ", type : " + type + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp +"]-----");

        UnifyResponse resp = new UnifyResponse();

        try
        {
            //参数校验
            if (!registerParaCheck(name, password, type, identityCode, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            String userType = type;
            if (StringUtils.isEmpty(type))
            {
                userType = Constants.USER_TYPE_MOBILE;
            }

            if (Constants.USER_TYPE_MOBILE.equals(userType) && StringUtils.isNotNull(identityCode))
            {
                if (!BaseService.containIdentityCodeSended(name))
                {
                    resp = new UnifyResponse();
                    resp.setCode(Constants.IDENTITY_CODE_TIME_OUT);
                    resp.setInfo(Constants.IDENTITY_CODE_TIME_OUT_INFO);
                    return resp;
                }
                else if (!BaseService.getIdentityCodeSended(name).getIdentityCode().equals(identityCode))
                {
                    resp = new UnifyResponse();
                    resp.setCode(Constants.IDENTITY_CODE_ERROR);
                    resp.setInfo(Constants.IDENTITY_CODE_ERROR_INFO);
                    return resp;
                }
                else
                {
                    BaseService.removeIdentityCodeSended(name);
                }
            }


            User user = new User();
            User userNew = new User();

            if (userType.equals(Constants.USER_TYPE_MOBILE))
            {
                user.setMobile(name);
            }
            else if (userType.equals(Constants.USER_TYPE_MAIL))
            {
                user.setMail(name);
            }

            User userExist = userService.findByName(name, userType);

            if (userExist != null)
            {
                resp.setCode(Constants.USER_EXSIT);
                resp.setInfo(Constants.USER_EXSIT_INFO);
            }
            else
            {
                user.setPassword(StringUtils.MD5(password));
                String curTime = System.currentTimeMillis() + "";
                user.setCreateTime(curTime);
                user.setUpdateTime(curTime);
                userService.saveUser(user);

                userNew = userService.findByName(name, userType);

                String sessionId = StringUtils.MD5(userNew.getId() + deviceId + timeStamp);
                userNew.setSessionId(sessionId);
                BaseService.addSession(sessionId, timeStamp);
                resp.setContent(userNew);
                resp.setCode(Constants.SUCCESS_CODE);
                resp.setInfo(Constants.SUCCESS_INFO);
            }

            logger.info("-----[register response :" + resp.toString()  + "]-----");
            return resp;
        }
        catch (Exception e)
        {
            logger.error("register error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
            return resp;
        }

    }

    /**
     *
     * 注册接口参数验证
     */
    private boolean registerParaCheck(String name, String passWord, String type, String identityCode, String deviceId, String timeStamp)
    {
        boolean result = true;

        if (StringUtils.isEmptyAndLen(name, Constants.name))
        {
            result = false;
        }
        else if (StringUtils.isEmptyAndLen(passWord, Constants.passWord))
        {
            result = false;
        }
        else if (StringUtils.checkLen(identityCode, Constants.identityCode))
        {
            result = false;
        }
        else if (!StringUtils.isEmpty(type) && (!Constants.USER_TYPE_MOBILE.equals(type) && !Constants.USER_TYPE_MAIL.equals(type)) )
        {
            result = false;
        }
        else if (!StringUtils.isEmpty(type) && Constants.USER_TYPE_MOBILE.equals(type) && name.length() > Constants.mobile)
        {
            result = false;
        }
         else if (!StringUtils.isEmpty(type) && Constants.USER_TYPE_MAIL.equals(type) && name.length() > Constants.mail)
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }
        return result;
    }

    /**
     * 登录接口
     * @param name 用户名
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param password 用户密码
     * @param type 用户名类型
     * @return resp 返回对象
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse login(@RequestParam(value="name", required = false) String name,
                               @RequestParam(value="deviceId", required = false) String deviceId,
                               @RequestParam(value="timeStamp", required = false) String timeStamp,
                               @RequestParam(value="password", required = false) String password,
                               @RequestParam(value="type", required = false) String type)
    {
        logger.info("-----[login: name : " + name + ", password : " + password + ", type : " + type + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp+ "]-----");

        if ("0".equals(BaseService.hasRun))
        {
            StartSendMsg sendMsg = new StartSendMsg();
            sendMsg.start();
            BaseService.hasRun = "1";
        }

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!loginParCheck(name, password, type, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            String userType = type;
            if (StringUtils.isEmpty(type))
            {
                userType = Constants.USER_TYPE_MOBILE;
            }

            // 是否是临时密码登陆
            if (Constants.USER_TYPE_MOBILE.equals(userType) && BaseService.containPasswordSended(name))
            {
                if (BaseService.getPasswordSended(name).getPassword().equals(password))
                {
                    userService.updatePassword(name, StringUtils.MD5(password));
                    BaseService.removePasswordSended(name);
                }
            }

            User user = userService.findByName(name, userType);
            String passwordMD5 = StringUtils.MD5(password);
            if(user == null)
            {
                user = new User();
                resp.setCode(Constants.USER_NOT_EXSIT);
                resp.setInfo(Constants.USER_NOT_EXSIT_INFO);
            }
            else if(user != null && !passwordMD5.equals(user.getPassword()))
            {
                user = new User();
                resp.setCode(Constants.USER_NOT_RIGHT);
                resp.setInfo(Constants.USER_NOT_RIGHT_INFO);
            }
            else
            {
                String sessionId = StringUtils.MD5(user.getId() + deviceId + timeStamp);
                BaseService.addSession(sessionId, timeStamp);
                user.setSessionId(sessionId);
                resp.setCode(Constants.SUCCESS_CODE);
                resp.setInfo(Constants.SUCCESS_INFO);
            }

            resp.setContent(user);

            logger.info("-----[login response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("login error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 登录接口参数验证
     */
    private boolean loginParCheck(String name, String password, String type, String deviceId, String timeStamp)
    {
        boolean result = true;

        if (StringUtils.isEmptyAndLen(name, Constants.name))
        {
            result = false;
        }
        else if (StringUtils.isEmptyAndLen(password, Constants.passWord))
        {
            result = false;
        }
        else if (!StringUtils.isEmpty(type) && (!Constants.USER_TYPE_MOBILE.equals(type) && !Constants.USER_TYPE_MAIL.equals(type)) )
        {
            result = false;
        }
        else if (Constants.USER_TYPE_MOBILE.equals(type) && name.length() > Constants.mobile)
        {
            result = false;
        }
        else if (Constants.USER_TYPE_MAIL.equals(type) && name.length() > Constants.mail)
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 登录接口
     * @param sessionId sessionId
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @return resp 返回对象
     */
    @RequestMapping(value = "/logout", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse logout(@RequestParam(value="sessionId", required = false) String sessionId,
                               @RequestParam(value="deviceId", required = false) String deviceId,
                               @RequestParam(value="timeStamp", required = false) String timeStamp){
        logger.info("-----[logout: sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp +"]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!logoutParCheck(sessionId, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            BaseService.removeSession(sessionId);

            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            logger.info("-----[logout response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("logout error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }

        return resp;
    }


    /**
     *
     * 登录接口参数验证
     */
    private boolean logoutParCheck(String sessionId, String deviceId, String timeStamp)
    {
        boolean result = true;

        if(StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 获取验证码接口
     * @param mobile mobile
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @return resp 返回对象
     */
    @RequestMapping(value = "/getIdentifyCode", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse getIdentifyCode(@RequestParam(value="mobile", required = false) String mobile,
                                         @RequestParam(value="deviceId", required = false) String deviceId,
                                         @RequestParam(value="timeStamp", required = false) String timeStamp)
    {
        logger.info("-----[getIdentifyCode: mobile : " + mobile + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp +"]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            if ("0".equals(BaseService.hasRun))
            {
                StartSendMsg sendMsg = new StartSendMsg();
                sendMsg.start();
                BaseService.hasRun = "1";
            }

            //参数校验
            if (!getIdentifyCodeParCheck(mobile, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            User user = userService.findByName(mobile, Constants.USER_TYPE_MOBILE);

            if(user != null)
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.USER_HAS_EXSITED);
                resp.setInfo(Constants.USER_HAS_EXSITED_INFO);
                return resp;
            }

            String identityCode = StringUtils.getRandomNum() + "";
            IdentityCode identity = new IdentityCode();
            identity.setIdentityCode(identityCode);
            identity.setTimeStamp(System.currentTimeMillis());
            identity.setMobile(mobile);
            BaseService.addIdentityCode(mobile, identity);

            Message message = new Message();
            message.setIdentityCode(identityCode);

            resp.setContent(message);
            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            logger.info("-----[getIdentifyCode response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("getIdentifyCode error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }

        return resp;
    }


    /**
     *
     * 获取验证码参数验证
     */
    private boolean getIdentifyCodeParCheck(String mobile, String deviceId, String timeStamp)
    {
        boolean result = true;

        if(StringUtils.isEmptyAndLen(mobile, Constants.mobile))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 获取密码接口
     * @param mobile mobile
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @return resp 返回对象
     */
    @RequestMapping(value = "/getPassword", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse getPassword(@RequestParam(value="mobile", required = false) String mobile,
                                     @RequestParam(value="deviceId", required = false) String deviceId,
                                     @RequestParam(value="timeStamp", required = false) String timeStamp)
    {
        logger.info("-----[getPassword: mobile : " + mobile + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp +"]-----");

        UnifyResponse resp = new UnifyResponse();

        try {

            if ("0".equals(BaseService.hasRun))
            {
                StartSendMsg sendMsg = new StartSendMsg();
                sendMsg.start();
                BaseService.hasRun = "1";
            }

            //参数校验
            if (!getPasswordCodeParCheck(mobile, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            User user = userService.findByName(mobile, Constants.USER_TYPE_MOBILE);

            if(user == null)
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.USER_HAS_NOT_EXSITED);
                resp.setInfo(Constants.USER_HAS_NOT_EXSITED_INFO);
                return resp;
            }

            String password = StringUtils.getRandomNum() + "";
            TemporaryUser temUser = new TemporaryUser();
            temUser.setMobile(mobile);
            temUser.setPassword(password);
            temUser.setTimeStamp(System.currentTimeMillis());
            BaseService.addPassword(mobile, temUser);

            resp.setContent(temUser);
            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            logger.info("-----[getPassword response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("getPassword error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }

        return resp;
    }

    /**
     *
     * 获取密码接口
     */
    private boolean getPasswordCodeParCheck(String mobile, String deviceId, String timeStamp)
    {
        boolean result = true;

        if(StringUtils.isEmptyAndLen(mobile, Constants.mobile))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 搜索用户接口
     * @param key 搜索关键字
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param begin 分页起始点
     * @param count 查询个数
     * @param sessionId sessionId
     * @return resp 返回对象
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse searchPerson(@RequestParam(value="deviceId", required = false) String deviceId,
                                      @RequestParam(value="timeStamp", required = false) String timeStamp,
                                      @RequestParam(value="sessionId", required = false) String sessionId,
                                      @RequestParam(value="key", required = false) String key,
                                      @RequestParam(value="begin", required = false) String begin,
                                      @RequestParam(value="count", required = false) String count)
    {
        logger.info("-----[searchFriend: key : " + key + ", begin : " + begin + ", count : " + count + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp+ "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!searchParCheck(key, begin, count, deviceId, timeStamp, sessionId))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            int beginNum = Constants.PAGE_BEGIN_DEFAULT;
            int countNum = Constants.PAGE_SIZE_DEFAULT;;
            if (!StringUtils.isEmpty(begin))
            {
                beginNum = Integer.parseInt(begin);
            }

            if (!StringUtils.isEmpty(count))
            {
                countNum = Integer.parseInt(count);
            }

            List<User> userList = userService.searchFriend(key, beginNum, countNum);

            long after;
            if (userList != null)
            {
                after = userService.searchFriendCount(key, beginNum + countNum);
                resp.setAfterCount(after);
            }

            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            resp.setContent(userList);

            logger.info("-----[searchFriend response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("searchFriend error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 搜索用户接口参数验证
     */
    private boolean searchParCheck(String key, String begin, String count, String deviceId, String timeStamp, String sessionId)
    {
        boolean result = true;

        if (StringUtils.isEmptyAndLen(key, Constants.SEARCH_FRIEDN_KEY_LEN))
        {
            result = false;
        }
        else if (StringUtils.checkLen(begin, Constants.SEARCH_BEGIN_LEN))
        {
            result = false;
        }
        else if (StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(count, Constants.SEARCH_COUNT_LEN))
        {
            result = false;
        }
        else if ((!StringUtils.isEmpty(count) && !StringUtils.isNumber(count)) || (!StringUtils.isEmpty(begin) && !!StringUtils.isNumber(begin)))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 好友列表接口
     * @param id 搜索关键字
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param sessionId 分页起始点
     * @return resp 返回对象
     */
    @RequestMapping(value = "/friends_list", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse getFridends(@RequestParam(value="deviceId", required = false) String deviceId,
                                    @RequestParam(value="sessionId", required = false) String sessionId,
                                    @RequestParam(value="timeStamp", required = false) String timeStamp,
                                    @RequestParam(value="id", required = false) String id)
    {
        logger.info("-----[getFriends: id : " + id + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp+ "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!getFriendsParCheck(id, sessionId, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            getFriendsInfo(id, Constants.FRIENDS_STATUS_FRIEND, resp);

            logger.info("-----[getFriends response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("getFriends error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 获取好友信息
     * @param id 搜索关键字
     * @param status 客户端设备id
     * @param resp 当前时间
     */
    private void getFriendsInfo(String id, String status, UnifyResponse resp)
    {
        List<Friends> friendsList = userService.getFriends(id, status);

        List<String> uid = new ArrayList<String>();

        for (Friends friend : friendsList)
        {
            uid.add(friend.getUserIdAdd());
        }

        List<User> userList = userService.findByUserIds(uid);

        resp.setCode(Constants.SUCCESS_CODE);
        resp.setInfo(Constants.SUCCESS_INFO);
        resp.setContent(userList);
    }

    /**
     * 粉丝列表接口
     * @param id 搜索关键字
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param sessionId 分页起始点
     * @return resp 返回对象
     */
    @RequestMapping(value = "/followers", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse getFollowers(@RequestParam(value="deviceId", required = false) String deviceId,
                                     @RequestParam(value="sessionId", required = false) String sessionId,
                                     @RequestParam(value="timeStamp", required = false) String timeStamp,
                                     @RequestParam(value="id", required = false) String id)
    {
        logger.info("-----[getFollowers: id : " + id + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp+ "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!getFriendsParCheck(id, sessionId, deviceId, timeStamp))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            getFriendsInfo(id, Constants.FRIENDS_STATUS_FOLLOW, resp);

            logger.info("-----[getFollowers response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("getFollowers error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 好友列表接口参数验证
     */
    private boolean getFriendsParCheck(String id, String sessionId, String deviceId, String timeStamp)
    {
        boolean result = true;

        if (StringUtils.isEmpty(id))
        {
            result = false;
        }
        if (StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }

        return result;
    }

    /**
     * 添加好友接口
     * @param id 主动添加人id
     * @param followId 被添加人id
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param sessionId sessionid
     * @return resp 返回对象
     */
    @RequestMapping(value = "/follow", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse follow(@RequestParam(value="deviceId", required = false) String deviceId,
                                @RequestParam(value="sessionId", required = false) String sessionId,
                                @RequestParam(value="timeStamp", required = false) String timeStamp,
                                @RequestParam(value="id", required = false) String id,
                                @RequestParam(value="followId", required = false) String followId)
    {
        logger.info("-----[follow: id : " + id + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp + ", followId : " + followId + "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!followParCheck(id, sessionId, deviceId, timeStamp, followId))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            Friends friends = userService.getFollow(id, followId);

            if (friends != null && !Constants.FRIENDS_STATUS_DELETE.equals(friends.getStatus()))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.YOU_ARE_FRIENDS);
                resp.setInfo(Constants.YOU_ARE_FRIENDS_INFO);
                return resp;
            }

            friends = userService.getFollow(followId, id);
            Friends friendsNew = new Friends();
            long curTime = System.currentTimeMillis();

            if (friends != null)
            {
                userService.updateFriendsStatus(friends.getUid(), Constants.FRIENDS_STATUS_FRIEND);
                friendsNew.setStatus(Constants.FRIENDS_STATUS_FRIEND);
                friendsNew.setAgreeTime(curTime);
            }
            else
            {
                friendsNew.setStatus(Constants.FRIENDS_STATUS_FOLLOW);
            }

            friendsNew.setUserIdAdd(id);
            friendsNew.setUserIdAgree(followId);
            friendsNew.setCreateTime(curTime);

            if (friends != null && Constants.FRIENDS_STATUS_DELETE.equals(friends.getStatus()))
            {
                userService.updateFriendsStatus(friends.getUid(), friendsNew.getStatus());
            }
            else
            {
                userService.saveFriends(friendsNew);
            }

            User user = userService.findByUserId(followId);

            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            resp.setContent(user);

            logger.info("-----[follow response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("follow error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 添加好友接口参数验证
     */
    private boolean followParCheck(String id, String sessionId, String deviceId, String timeStamp, String followId)
    {
        boolean result = true;

        if (StringUtils.isEmpty(id))
        {
            result = false;
        }
        else if (StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }
        else if (StringUtils.isEmpty(followId))
        {
            result = false;
        }

        return result;
    }

    /**
     * 删除好友接口
     * @param id 搜索关键字
     * @param deviceId 客户端设备id
     * @param timeStamp 当前时间
     * @param sessionId 分页起始点
     * @return resp 返回对象
     */
    @RequestMapping(value = "/unfollow", method = RequestMethod.POST, produces = "application/json")
    public UnifyResponse unfollow(@RequestParam(value="deviceId", required = false) String deviceId,
                                @RequestParam(value="sessionId", required = false) String sessionId,
                                @RequestParam(value="timeStamp", required = false) String timeStamp,
                                @RequestParam(value="id", required = false) String id,
                                @RequestParam(value="followId", required = false) String followId)
    {
        logger.info("-----[unfollow: id : " + id + ", sessionId : " + sessionId + ", deviceId : " + deviceId + ", timeStamp : " + timeStamp + ", followId : " + followId + "]-----");

        UnifyResponse resp = new UnifyResponse();

        try {
            //参数校验
            if (!unfollowParCheck(id, sessionId, deviceId, timeStamp, followId))
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.PARAM_CHECK_FAIL);
                resp.setInfo(Constants.PARAM_CHECK_FAIL_INFO);
                return resp;
            }

            boolean session = BaseService.containSession(sessionId);

            if (!session)
            {
                resp.setCode(Constants.SESSION_TIME_OUT);
                resp.setInfo(Constants.SESSION_TIME_OUT_INFO);
                return resp;
            }

            Friends friends = userService.getFollow(id, followId);

            if (friends == null)
            {
                resp = new UnifyResponse();
                resp.setCode(Constants.YOU_ARE_NOT_FRIENDS);
                resp.setInfo(Constants.YOU_ARE_NOT_FRIENDS_INFO);
                return resp;
            }

            userService.updateFriendsInfo(id, followId, Constants.FRIENDS_STATUS_DELETE);

            friends = userService.getFollow(followId, id);

            if (friends != null)
            {
                userService.updateFriendsInfo(followId, id, Constants.FRIENDS_STATUS_FOLLOW);
            }

            User user = userService.findByUserId(followId);
            resp.setCode(Constants.SUCCESS_CODE);
            resp.setInfo(Constants.SUCCESS_INFO);
            resp.setContent(user);

            logger.info("-----[unfollow response :" + resp.toString()  + "]-----");
        }
        catch (Exception e)
        {
            logger.error("unfollow error : " + e.getMessage());
            resp = new UnifyResponse();
            resp.setCode(Constants.SERVER_ERROR);
            resp.setInfo(Constants.SERVER_ERROR_INFO);
        }
        return resp;
    }

    /**
     * 删除好友接口参数验证
     */
    private boolean unfollowParCheck(String id, String sessionId, String deviceId, String timeStamp, String followId)
    {
        boolean result = true;

        if (StringUtils.isEmpty(id))
        {
            result = false;
        }
        else if (StringUtils.isEmpty(sessionId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(deviceId, Constants.deviceId))
        {
            result = false;
        }
        else if (StringUtils.checkLen(timeStamp, Constants.timeStamp))
        {
            result = false;
        }
        else if (StringUtils.isEmpty(followId))
        {
            result = false;
        }

        return result;
    }
}
